package com.app.service.impl;
import com.app.db.mongodb.MongoUserEntity;
import com.app.service.MongoService;
import com.mongodb.WriteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.*;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author liuli
 * 参考文档
 * https://docs.spring.io/spring-data/mongodb/docs/3.0.1.RELEASE/reference/html/#reference
 */
@Service
public class MongoServiceImpl  implements MongoService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Long id) {
        MongoUserEntity mMongoUserEntity =  new MongoUserEntity();
        mMongoUserEntity.setId(id);
        mMongoUserEntity.setTitle("用户");
        mMongoUserEntity.setMsg("这是李白的诗文");
        mMongoUserEntity.setMoney(20.1);
        mMongoUserEntity.setMakeDate(new Date());
        mongoTemplate.save(mMongoUserEntity);
    }

    @Override
    public void update(Long id, String name) {
        Query query=new Query(Criteria.where("id").is(id));
        Update update= new Update().set("name", name);
        WriteResult result= mongoTemplate.updateFirst(query,update,MongoUserEntity.class);
        System.out.println("成功更新："+result.getN()+"条 ,数据是否存在："+result.isUpdateOfExisting());
    }

    @Override
    public void del(Long id) {
        Query query=new Query(Criteria.where("id").is(id));
        WriteResult result = mongoTemplate.remove(query,MongoUserEntity.class);
        System.out.println("成功删除："+result.getN()+"条 ,数据是否存在："+result.isUpdateOfExisting());
    }

    /**
     *
     * For example, given these documents (and assuming the title attribute is text-indexed):
     *语言支持（企业版支持中文）
     * { "id": 1, "title": "The days of the week"}
     * { "id": 2, "title": "Once a week"}
     * { "id": 3, "title": "Once a month"}
     *  matchingAny("Once") will match the documents with id=2 and id=3
     *  matchingAny("month", "foo' , "bar") will match the document with id=3
     *  matchingPhrase("The days of the week") will match the document with id=1
     *
     * @param pageSize
     * @param pageNum
     * @param name
     * @return
     */
    @Override
    public  List<MongoUserEntity> get(Integer pageSize, Integer pageNum, String name) {
        //查询 (关键字查询)
        //Query query =  TextQuery.queryText(new TextCriteria().matchingAny(name));
        //查询 (短语查询)
       // Query query =  TextQuery.queryText(new TextCriteria().matching("\""+name+"\""));
        Query query =  TextQuery.query(new TextCriteria().matching("\""+name+"\""));
        //模糊查询
       // Pattern pattern =  Pattern.compile("^.*"+name+".*$", Pattern.CASE_INSENSITIVE);
        //完全匹配  Pattern pattern = Pattern.compile("^张$", Pattern.CASE_INSENSITIVE);
        //右匹配  Pattern pattern = Pattern.compile("^.*张$", Pattern.CASE_INSENSITIVE);
        //左匹配  Pattern pattern = Pattern.compile("^张.*$", Pattern.CASE_INSENSITIVE);
        //模糊匹配 Pattern pattern = Pattern.compile("^.*张.*$", Pattern.CASE_INSENSITIVE);
       // query.addCriteria(Criteria.where("name").regex(pattern));

        //分页
       // query.skip(pageNum*pageSize).limit(pageSize);
        //排序
        //query.with(new Sort(Sort.Direction.DESC,"makeDate"));

        List<MongoUserEntity> mongoDBInfoBeanList= mongoTemplate.find(query,MongoUserEntity.class);
        System.out.println("查询结果："+mongoDBInfoBeanList.size());
        return mongoDBInfoBeanList;
    }

    @Override
    public void saveMuli(Object obj) {
        MongoUserEntity mMongoUserEntityM1 =  new MongoUserEntity();
        mMongoUserEntityM1.setId(System.currentTimeMillis());
        mMongoUserEntityM1.setMoney(20.1);
        mMongoUserEntityM1.setMakeDate(new Date());
    }
}
